package com.operation.controller;

import com.operation.entity.UserInfo;
import com.operation.service.UserService;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AssertionHolder;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PageController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String page() {
        String page = null;
        UserInfo userInfo = new UserInfo();
        Assertion assertion = AssertionHolder.getAssertion();
        AttributePrincipal ap = assertion.getPrincipal();
        if (userService.isExist(Long.valueOf(ap.getName())) == 1) {
            System.out.println("++++++++++++++++++++++++++++++用户不存在");
            /*userInfo.setUserAccount(Long.valueOf(ap.getName()));
            userService.add(userInfo);*/
        } else {
            userInfo.setUserAccount(Long.valueOf(ap.getName()));
            userInfo.setUserPassword("1");
            if (userService.isIt(userInfo) != null) {
                userService.updateIsOnline(userInfo);
            }
            System.out.println("++++++++++++++++++++++++++++++用户已存在");
        }
        return "userFirst";
    }

    @RequestMapping(value = "/detail/{jobNumber}", method = RequestMethod.GET)
    public String pageDetail(Model model, @PathVariable String jobNumber) {
        model.addAttribute("jobNumber", jobNumber);
        return "message_detail";
    }

    @RequestMapping(value = "/onlinePersonnel/{jobNumber}/{receiveOne}", method = RequestMethod.GET)
    public String onlinePersonnel(Model model, @PathVariable String jobNumber, @PathVariable String receiveOne) {
        model.addAttribute("jobNumber", jobNumber);
        model.addAttribute("receiveOne", receiveOne);
        return "go_operators";
    }
}
